package Dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.log.Log;

import Dao.DBConn;
import Model.CardBean;
 
public class CardDAO {
	
	private Connection conn;
	 
    public CardDAO() throws Exception {
        conn = DBConn.getConnection();
    }
    //根据ID查询Card信息
    public List<CardBean> getCard(int CardID) throws Exception{
        List<CardBean> ret = new ArrayList<CardBean>();
        try{
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from card where CARD_ID='"+ CardID + "'");
            while (rs.next()){
            	CardBean comp = new CardBean();
            	comp.setID(rs.getInt("CARD_ID"));
                comp.setName(rs.getString("CARD_NAME"));
                comp.setCost(rs.getFloat("CARD_COST"));                
                ret.add(comp);
            }
            rs.close();
            stmt.close();
            conn.close();
        }catch (Exception e){
            e.getMessage();
        }
        return ret;
    }
    //根据卡名查询卡信息
    public List<CardBean> getCardByName(String CardName) throws Exception{
        List<CardBean> ret = new ArrayList<CardBean>();
        try{
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from card where CARD_NAME='"+ CardName + "'");
            while (rs.next()){
            	CardBean comp = new CardBean();
            	comp.setID(rs.getInt("CARD_ID"));
                comp.setName(rs.getString("CARD_NAME"));
                comp.setCost(rs.getFloat("CARD_COST"));                
                ret.add(comp);
            }            
            rs.close();
            stmt.close();
            conn.close();
        }catch (Exception e){
            e.getMessage();
        }
        return ret;
    }
    
    //添加新Card
    public void addCard(CardBean card) throws Exception{
        String sql = "insert into card(CARD_ID,CARD_NAME,CARD_COST) values(?,?,?)";
        try{
            PreparedStatement prestmt = conn.prepareStatement(sql);
            prestmt.setInt(1, card.getID());
            prestmt.setString(2, card.getName());
            prestmt.setFloat(3, card.getCost());            
            prestmt.execute();
            prestmt.close();
            conn.close();
        }catch (Exception e){
            e.getMessage();
        }
    }
 
}


